package com.ctg.itrdc.sysmgr.portal.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ctg.itrdc.pasp.context.service.BaseService;
import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemRolesDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemRolesService;
import com.ctg.itrdc.sysmgr.portal.data.ISystemRolesDao;
import com.ctg.itrdc.sysmgr.portal.data.entity.SystemRoles;
import com.ctg.itrdc.sysmgr.portal.data.vo.SystemRolesVO;
import com.ctg.itrdc.sysmgr.portal.service.exception.SysmgrServiceException;
import com.ctg.itrdc.sysmgr.portal.service.utils.BeanUtils;

@Component("systemRolesService")
public class SystemRolesServiceImpl  extends BaseService<SystemRoles, Long, SystemRolesDTO>  implements ISystemRolesService {
    @Resource
    private ISystemRolesDao systemRolesDao;
    
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Override
	public Long insert(SystemRolesDTO dto) {
    	try {
			SystemRoles entity = BeanUtils.applyIfNotException(dto,SystemRoles.class);
			long id = systemRolesDao.insert(entity);
			//去掉 角色编号自动生成的逻辑 xkw 20160808
//    	String sysRoleCode = RandomCodeUtil.gen(RandomCodeUtil.SYSTEM_ROLE, id);
//    	SystemRolesDTO tmp = new SystemRolesDTO();
//    	tmp.setId(id);
//    	tmp.setSysRoleCode(sysRoleCode);
//    	this.updateNotNullFieldsByPrimaryKey(tmp);
			return id;
    	} catch (DuplicateKeyException e) {
			throw new SysmgrServiceException("角色编码重复");
		}
	}
    
    @Override
	public int updateByPrimaryKey(SystemRolesDTO dto) {
		try {
			return super.updateByPrimaryKey(dto);
		} catch (DuplicateKeyException e) {
			throw new SysmgrServiceException("角色编码重复");
		}
	}

	@Override
    public List<SystemRolesDTO> selectBySysUserId(Long sysUserId){
    	List<SystemRoles> entityList = systemRolesDao.selectBySysUserId(sysUserId);
    	return BeanUtils.copyListNotException(entityList, SystemRolesDTO.class);
    }
    
    @Override
    public List<SystemRolesDTO> selectBySysPostId(Long sysPostId){
    	List<SystemRoles> entityList = systemRolesDao.selectBySysPostId(sysPostId);
    	return BeanUtils.copyListNotException(entityList, SystemRolesDTO.class);
    };
    
    @Override
    public Long[] selectRoleIdsBySysUserId(Long sysUserId){
		return systemRolesDao.selectRoleIdsBySysUserId(sysUserId);
	}
    
    @Override
	public Long[] selectRoleIdsBySysPostId(Long sysPostId){
		return systemRolesDao.selectRoleIdsBySysPostId(sysPostId);
	}
    
    @Override
	public List<String> getRolesName(Long sysPostId, Long sysUserId){
		List<String> rolesName = new ArrayList<>();
		if(sysUserId!=null){
			List<SystemRolesDTO> uIdRoleList = this
					.selectBySysUserId(sysUserId);
			addRoles(uIdRoleList, rolesName);
		}
		if (sysPostId != null) {
			List<SystemRolesDTO> pIdRoleList = this
					.selectBySysPostId(sysPostId);
			addRoles(pIdRoleList, rolesName);
		}
		logger.debug("find rolesName from DB,param:sysUserId:{},sysPostId:{},result:{}",
				sysUserId,sysPostId, JSON.toJSONString(rolesName, true));

		return rolesName;
	}
    
    /**
	 * 添加角色
	 * 
	 * @param roleIdList
	 * @param rolesName
	 */
	private void addRoles(List<SystemRolesDTO> roleList, List<String> rolesName) {
		if (roleList == null) {
			return;
		}
		for (SystemRolesDTO dto : roleList) {
			rolesName.add(dto.getSysRoleName());
		}
	}
	
	@SuppressWarnings("all")
	@Override
	public PageInfo<SystemRolesDTO> queryPage4Post(SystemRolesDTO dto,Long sysPostId,
			Integer currentPage, Integer perPageNum) {
		
		SystemRoles entity = BeanUtils.applyIfNotException(dto,
				SystemRoles.class);
		PageInfo page = systemRolesDao.queryPageInfo4Post(entity, sysPostId,currentPage, perPageNum);
		List<SystemRolesVO> list = page.getList();
		if(CollectionUtils.isNotEmpty(list)){
			List<SystemRolesDTO> resultList = BeanUtils.copyListNotException(list, SystemRolesDTO.class);
			page.setList(resultList);
		}
		return page;
	}
	
	@SuppressWarnings("all")
	@Override
	public PageInfo<SystemRolesDTO> queryPage4User(SystemRolesDTO dto,Long sysUserId,
			Integer currentPage, Integer perPageNum) {
		
		SystemRoles entity = BeanUtils.applyIfNotException(dto,
				SystemRoles.class);
		PageInfo page = systemRolesDao.queryPageInfo4User(entity,sysUserId, currentPage, perPageNum);
		List<SystemRolesVO> list = page.getList();
		if(CollectionUtils.isNotEmpty(list)){
			List<SystemRolesDTO> resultList = BeanUtils.copyListNotException(list, SystemRolesDTO.class);
			page.setList(resultList);
		}
		return page;
	}
	
	@Transactional
	public int deleteByPrimaryKey(SystemRolesDTO dto) {
		return super.deleteByPrimaryKey(dto);
	}
	
	@Transactional
	public int deleteByPrimaryKey(Long id) {
		return super.deleteByPrimaryKey(id);
	}
	
	@Transactional
	public int[] batchDeleteByPrimaryKey(List<SystemRolesDTO> list) {
		return batchDeleteByPrimaryKey(list);
	}
	
}
